package com.cetccloud.ap.controller.model;

import com.cetccloud.ap.util.ResultEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName ApiResult
 * @Description TODO
 * @Author superz
 * @Date 2021/4/21 14:45
 * @Version 1.0
 **/
@Data
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回码(200:成功; 非200:失败)
     **/
    private Integer code;

    /**
     * 成功返回“成功”，失败返回对应的失败信息
     **/
    private String msg;

    /**
     * 响应体
     **/
    private T body;

    public static ApiResult success() {
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(ResultEnum.SUCCESS.getCode());
        apiResult.setMsg(ResultEnum.SUCCESS.getMsg());
        return apiResult;
    }

    public static ApiResult error() {
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(ResultEnum.ERROR.getCode());
        apiResult.setMsg(ResultEnum.ERROR.getMsg());
        return apiResult;
    }

    public static ApiResult error(String msg) {
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(ResultEnum.ERROR.getCode());
        apiResult.setMsg(msg);
        return apiResult;
    }

    public ApiResult<T> build(T result) {
        this.body = result;
        return this;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return ResultEnum.SUCCESS.getCode().equals(this.getCode());
    }
}
